using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using Microsoft.LearningComponents;
using Microsoft.LearningComponents.Storage;
using Microsoft.SharePointLearningKit;
using Schema = Microsoft.SharePointLearningKit.Schema;
namespace AddInstructorsSample
{
public class AddInstructorsPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel SuccessPanel;
protected System.Web.UI.WebControls.Label SuccessLabel;
protected Microsoft.SharePoint.WebControls.InputFormSection OriginalInstructorSection;
protected System.Web.UI.WebControls.DropDownList OriginalInstructor;
protected Microsoft.SharePoint.WebControls.InputFormSection NewInstructorsSection;
protected Microsoft.SharePoint.WebControls.PeopleEditor NewInstructors;
protected Microsoft.SharePoint.WebControls.ButtonSection ButtonSection;
protected System.Web.UI.WebControls.Button BtnOk;
protected override void OnLoad(EventArgs e)
{
if (!SPFarm.Local.CurrentUserIsAdministrator())
{
throw new UnauthorizedAccessException(
"Access is denied. Only adminstrators can access this page.");
}
using (new LearningStorePrivilegedScope())
{
if (IsPostBack)
return;
using (SPWeb spWeb = SPControl.GetContextWeb(HttpContext.Current))
{
SlkStore slkStore = SlkStore.GetStore(spWeb);
LearningStoreJob job = slkStore.LearningStore.CreateJob();
LearningStoreQuery query = slkStore.LearningStore.CreateQuery(
"AllAssignmentInstructors");
query.SetParameter("SPSiteGuid", spWeb.Site.ID);
query.AddColumn("InstructorName");
query.AddColumn("InstructorId");
query.AddSort("InstructorName", LearningStoreSortDirection.Ascending);
job.PerformQuery(query);
DataRowCollection rows = job.Execute<DataTable>().Rows;
OriginalInstructor.Items.Add(String.Empty);
foreach (DataRow row in rows)
{
ListItem listItem = new ListItem();
listItem.Text = (string) row["InstructorName"];
UserItemIdentifier originalInstructorId =
new UserItemIdentifier((LearningStoreItemIdentifier)row["InstructorId"]);
listItem.Value = originalInstructorId.GetKey().ToString(
CultureInfo.InvariantCulture);
OriginalInstructor.Items.Add(listItem);
}
}
}
}
protected void BtnOk_Click(object sender, EventArgs e)
{
if (!SPFarm.Local.CurrentUserIsAdministrator())
{
throw new UnauthorizedAccessException(
"Access is denied. Only adminstrators can access this page.");
}
using (new LearningStorePrivilegedScope())
{
if (OriginalInstructor.SelectedValue.Length == 0)
return;
if (NewInstructors.Accounts.Count == 0)
return;
UserItemIdentifier originalInstructorId = new UserItemIdentifier(
long.Parse(OriginalInstructor.SelectedValue, CultureInfo.InvariantCulture));
using (SPWeb spWeb = SPControl.GetContextWeb(HttpContext.Current))
{
List<AssignmentItemIdentifier> assignmentIds = new List<AssignmentItemIdentifier>();
SlkStore slkStore = SlkStore.GetStore(spWeb);
LearningStoreJob job = slkStore.LearningStore.CreateJob();
LearningStoreQuery query = slkStore.LearningStore.CreateQuery("AllAssignmentIds");
query.AddCondition("SPSiteGuid", LearningStoreConditionOperator.Equal,
spWeb.Site.ID);
query.AddCondition("InstructorId", LearningStoreConditionOperator.Equal,
originalInstructorId);
query.AddColumn("AssignmentId");
job.PerformQuery(query);
DataRowCollection rows = job.Execute<DataTable>().Rows;
OriginalInstructor.Items.Add(String.Empty);
foreach (DataRow row in rows)
{
assignmentIds.Add(new AssignmentItemIdentifier(
(LearningStoreItemIdentifier)row["AssignmentId"]));
}
List<UserItemIdentifier> newInstructorIds = new List<UserItemIdentifier>();
foreach (string loginName in NewInstructors.Accounts)
{
SPUser spUser;
try
{
spUser = spWeb.AllUsers[loginName];
}
catch (SPException)
{
NewInstructors.ErrorMessage = "User isn't in \"All People\": " +
loginName;
return;
}
string userKey = String.IsNullOrEmpty(spUser.Sid)
? spUser.LoginName : spUser.Sid;
UserItemIdentifier userId;
job = slkStore.LearningStore.CreateJob();
query = slkStore.LearningStore.CreateQuery("UserItem");
query.AddCondition("Key", LearningStoreConditionOperator.Equal, userKey);
query.AddColumn("Id");
job.PerformQuery(query);
rows = job.Execute<DataTable>().Rows;
if (rows.Count != 0)
{
userId = new UserItemIdentifier(
(LearningStoreItemIdentifier) rows[0]["Id"]);
}
else
{
job = slkStore.LearningStore.CreateJob();
Dictionary<string,object> findProperties = new Dictionary<string,object>();
findProperties["Key"] = userKey;
Dictionary<string,object> setProperties = new Dictionary<string,object>();
setProperties["Name"] = spUser.Name;
job.AddOrUpdateItem("UserItem", findProperties, setProperties, null, true);
userId = new UserItemIdentifier(
job.Execute<LearningStoreItemIdentifier>());
}
newInstructorIds.Add(userId);
}
Dictionary<UserItemIdentifier, bool> oldInstructors =
new Dictionary<UserItemIdentifier, bool>();
int updatedAssignmentCount = 0;
foreach (AssignmentItemIdentifier assignmentId in assignmentIds)
{
AssignmentProperties assignmentProperties =
slkStore.GetAssignmentProperties(assignmentId, SlkRole.Instructor);
oldInstructors.Clear();
foreach (SlkUser slkUser in assignmentProperties.Instructors)
oldInstructors[slkUser.UserId] = true;
int oldInstructorCount = oldInstructors.Count;
foreach (UserItemIdentifier userId in newInstructorIds)
{
if (!oldInstructors.ContainsKey(userId))
assignmentProperties.Instructors.Add(new SlkUser(userId));
}
if (assignmentProperties.Instructors.Count != oldInstructorCount)
{
slkStore.SetAssignmentProperties(assignmentId, assignmentProperties);
updatedAssignmentCount++;
}
}
SuccessPanel.Visible = true;
SuccessLabel.Text =
String.Format("Found {0} assignment(s); updated {1} assignment(s).",
assignmentIds.Count, updatedAssignmentCount);
OriginalInstructorSection.Visible = false;
NewInstructorsSection.Visible = false;
ButtonSection.Visible = false;
}
}
}
}
}